﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using ZhouYu.DreamTour.BLL.IService;
using ZhouYu.DreamTour.Core;
using ZhouYu.DreamTour.Model;
using ZhouYu.DreamTour.ViewModel;
using ZhouYu.DreamTour.Web.Models;

namespace ZhouYu.DreamTour.Web.Controllers
{
    public class LoginController : Controller
    {
        private ISmsInfoService _smsInfoService;
        private IUserService _userService;
        public LoginController(ISmsInfoService smsInfoService, IUserService userService)
        {
            _smsInfoService = smsInfoService;
            _userService = userService;
        }


        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public JsonResult LoginUserInfo(UserInfo userInfo)
        {
            //创建响应结果类对象
            ResponseResult responseResult = _userService.LoginUserInfo(userInfo);
            //创建UserInfo类对象调用用户登录
            //responseResult.data = _userService.LoginUserInfo(userInfo);
            responseResult.Success = responseResult.data != null;
            //如果登录成功则修改他的Ip和上一次登录时间
            if (responseResult.Success)
            {              
                    //记住密码
                    if (userInfo.User_IsCheck)
                    {
                        //判断是否记住密码
                        string userData = responseResult.data.User_Id + "#" + responseResult.data.User_Name + "#" + responseResult.data.User_Pwd;
                        //数据放入ticket
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, responseResult.data.User_Name, DateTime.Now, DateTime.Now.AddDays(30), true, userData);
                        //数据加密
                        string enyTicket = FormsAuthentication.Encrypt(ticket);
                        //创建cookie对象
                        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, enyTicket);
                        if (ticket.IsPersistent)
                        {
                            cookie.Expires = ticket.Expiration;
                        }
                        //将cookie存入响应上下文中
                        System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
                        UserInfoContext.userInfoContext.UserInfo = responseResult.data;
                    }
                    else
                    {
                        //不记住密码则把信息存在Session中
                        UserInfoContext.userInfoContext.UserInfo = responseResult.data;
                    }
                }
            return Json(responseResult);                
        }



        //退出登录
        public ActionResult LoginOut()
        {
            //清除Session
            UserInfoContext.userInfoContext.UserInfo = null;
            //退出
            FormsAuthentication.SignOut();
            return Redirect("/Home/User_Login");
        }
    }
}
